package com.wz.config;

import com.wz.dao.TransactionDao;
import com.wz.pojo.Block;
import com.wz.pojo.Transaction;
import com.wz.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.generated.Bytes32;
import org.web3j.abi.datatypes.generated.Uint32;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.http.HttpService;

import java.util.Arrays;

import static org.web3j.protocol.core.DefaultBlockParameterName.EARLIEST;
import static org.web3j.protocol.core.DefaultBlockParameterName.LATEST;

/**
 * @program: demo
 * @description:
 * @author: Wangzheng
 * @create: 2020-12-24 16:04
 **/
@Component
public class Listen implements ApplicationRunner {
	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("开始执行监听操作------------");
		test();
	}

	@Autowired
	private Transaction transaction;
	
	@Autowired
	private TransactionDao transactionDao;
	
	@Autowired
	private TransactionService transactionService;

	public void test() {
		Block block = new Block();
		Web3j web3j = Web3j.build(new HttpService(Normal.SERVER));
		Event event = new Event("Transfer",
				Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {
				}),
				Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {
												},
						new TypeReference<Uint32>() {
						}));
		EthFilter filter = new EthFilter(EARLIEST, LATEST, Normal.CONTRACTADDRESS);
		filter.addSingleTopic(EventEncoder.encode(event));
		/*web3j.ethLogObservable(filter).subscribe(log -> {
			System.out.println(1);
		});

		web3j.catchUpToLatestBlockObservable(LATEST, true).subscribe(tx -> {
			System.out.println(2);
		});

		web3j.transactionObservable().subscribe(x -> {
			System.out.println("当前块号" + " : " + x.getBlockNumber());
			
		});

		web3j.catchUpToLatestBlockObservable(LATEST, true).subscribe(t -> {
			System.out.println(4);
		});

		web3j.blockObservable(false).subscribe(b->{
			System.out.println(5);
		});
*/
		web3j.catchUpToLatestAndSubscribeToNewTransactionsObservable(LATEST).subscribe(tx -> {
			System.out.println("监听开始:");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			transactionService.insertDBByLastBlockNum(tx.getBlockNumber());
			System.out.println(tx.getBlockNumber());
		});
		
	}


}















/*
* 
* 
		Web3j.build(new HttpService(Normal.SERVER)).replayBlocksObservable(
				DefaultBlockParameter.valueOf(String.valueOf(EARLIEST)),
				DefaultBlockParameter.valueOf(String.valueOf(LATEST)),
				false).subscribe(ethBlock -> {
			//定义时间类型转换:		
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//获取从块里拿到的时间戳:
			int time = ethBlock.getBlock().getTimestamp().intValue();
			//转换时间戳为具体日期时间:
			String timestamp = sdf.format(new Date(time * 1000L));


			Block block = new Block();
			block
					.setNumber(ethBlock.getBlock().getNumber())
					.setHash(ethBlock.getBlock().getHash())
					.setParentHash(ethBlock.getBlock().getParentHash())
					.setMiner(ethBlock.getBlock().getMiner())
					.setDifficulty(ethBlock.getBlock().getDifficulty())
					.setGasLimit(ethBlock.getBlock().getGasLimit())
					.setGasUsed(ethBlock.getBlock().getGasUsed())
					.setTimestamp(timestamp)
					.setNumber(ethBlock.getBlock().getNumber())
					.setSize(ethBlock.getBlock().getSize());
			blockDao.insertBlock(block);
			System.out.println(block);
		});*/